package com.wm.blog.center.config;

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;

/**
 * @Author: weiming
 * @Date: 2021/5/20 14:42
 * @Description:
 */
@Configuration
public class ElasticsearchConfig {
//    @Bean
//    public RestHighLevelClient restHighLevelClient() {
//        RestHighLevelClient client = new RestHighLevelClient(
//                RestClient.builder(
//                        new HttpHost("127.0.0.1", 9200, "http")
//                        //多个节点也是在当前地方配置
//                        //, new HttpHost("localhost", 9300, "http")
//                ));
//        return client;
//    }

    @Value("${butter-blog.elasticsearch.address:127.0.0.1:9200}")
    private String address = "127.0.0.1:9200";

    /**
     *  连接超时时间
     */
    @Value("${butter-blog.elasticsearch.connect-timeout:1000}")
    private int connectTimeOut = 1000;

    /**
     * 连接超时时间
     */
    @Value("${butter-blog.elasticsearch.socket-timeout:30000}")
    private int socketTimeOut = 30000;

    /**
     * 获取连接的超时时间
     */
    @Value("${butter-blog.elasticsearch.connection-request-timeout:500}")
    private int connectionRequestTimeOut = 500;

    /**
     * 最大连接数
     */
    @Value("${butter-blog.elasticsearch.max-connect-num:100}")
    private int maxConnectNum = 100;

    /**
     * 最大路由连接数
     */
    @Value("${butter-blog.elasticsearch.max-connect-per-route:100}")
    private int maxConnectPerRoute = 100;


    @Bean
    RestHighLevelClient restHighLevelClient() {
        ArrayList<HttpHost> hostList = new ArrayList<>();
        String[] addrss = address.split(",");
        for(String addr : addrss){
            String[] arr = addr.split(":");
            hostList.add(new HttpHost(arr[0], Integer.parseInt(arr[1]), "http"));
        }

        RestClientBuilder builder = RestClient.builder(hostList.toArray(new HttpHost[0]));
        // 异步httpclient连接延时配置
        builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
            @Override
            public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
                requestConfigBuilder.setConnectTimeout(connectTimeOut);
                requestConfigBuilder.setSocketTimeout(socketTimeOut);
                requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
                return requestConfigBuilder;
            }
        });
        // 异步httpclient连接数配置
        builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
            @Override
            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                httpClientBuilder.setMaxConnTotal(maxConnectNum);
                httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
                return httpClientBuilder;
            }
        });

        RestHighLevelClient client = new RestHighLevelClient(builder);
        return client;
    }
}
